package com.example.sql;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PNOperator {
	String TAG="MOperator";//LogCat标签
	SqlHelp dbHelp;//数据库适配变量
	SQLiteDatabase db;//数据库访问变量
	//构造函数
	public PNOperator(Context context) {
		// TODO 自动生成的构造函数存根
		this.dbHelp=new SqlHelp(context, SqlHelp.DBName);
	}
	
	//个体信息插入到数据库信息表中
		public void insert(PNBean pnbean){
			ContentValues values=new ContentValues();
			values.put("name", pnbean.getName());
			values.put("phoneNumber", pnbean.getPhoneNumber());
			values.put("shortNumber", pnbean.getShortNumber());
			values.put("grade", pnbean.getGrade());
			values.put("qq", pnbean.getQq());
			values.put("weixin", pnbean.getWeixin());
			db=dbHelp.getWritableDatabase();
			db.insert(SqlHelp.TableNumber, null, values);
			db.close();
			values.clear();
		}
		//更新数据库信息表个体信息
		public void update(PNBean pnbean){
			ContentValues values=new ContentValues();
			values.put("name", pnbean.getName());
			values.put("phoneNumber", pnbean.getPhoneNumber());
			values.put("shortNumber", pnbean.getShortNumber());
			values.put("grade", pnbean.getGrade());
			values.put("qq", pnbean.getQq());
			values.put("weixin", pnbean.getWeixin());
			db=dbHelp.getWritableDatabase();
			db.update(SqlHelp.TableNumber, values, "id=?", new String[]{pnbean.getId()+""});
			db.close();
			values.clear();
		}
		//删除数据库信息表中指定编号信息
		public void delete(int id){
			db=dbHelp.getWritableDatabase();
			db.delete(SqlHelp.TableNumber,  "id=?",  new String[]{id+""});
			db.close();

		}
		//id  姓名  电话  短号  班级  qq  微信
		//获取信息表中所有信息
		public ArrayList<PNBean> quary(){	
			Log.i(TAG+"quary", "Start");
			ArrayList<PNBean> list=new ArrayList<PNBean>();
			PNBean bean=null;
			db=dbHelp.getReadableDatabase();
			Cursor cursor;
			cursor=db.query(SqlHelp.TableNumber, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				bean=new PNBean();
				bean.setId(cursor.getInt(0));
				bean.setName(cursor.getString(1));
				bean.setPhoneNumber(cursor.getString(2));
				bean.setShortNumber(cursor.getString(3));
				bean.setGrade(cursor.getString(4));
				bean.setQq(cursor.getString(5));
				bean.setWeixin(cursor.getString(6));
				list.add(bean);
			}
			db.close();
			cursor.close();
			Log.i(TAG+"quary", "return");
			return list;
		}
		//获取信息表中指定信息编号的信息
		public ArrayList<PNBean> quary(int id){		
			ArrayList<PNBean> list=new ArrayList<PNBean>();
			PNBean bean=null;
			db=dbHelp.getReadableDatabase();
			Cursor cursor;
			cursor=db.query(SqlHelp.TableNumber, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				if (cursor.getInt(0)==id) {
					bean=new PNBean();
					bean.setId(cursor.getInt(0));
					bean.setName(cursor.getString(1));
					bean.setPhoneNumber(cursor.getString(2));
					bean.setShortNumber(cursor.getString(3));
					bean.setGrade(cursor.getString(4));
					bean.setQq(cursor.getString(5));
					bean.setWeixin(cursor.getString(6));
					list.add(bean);
				}
				
			}
			db.close();
			cursor.close();
			return list;
		}
}
